package controller.dictionary;

import controller.common.BaseController;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.LeaveReasonServices;
import validator.NotBlankFluentValidator;
import wt.entity.dictionary.LeaveReason;
import wt.entity.dictionary.LeaveReasonExample;
import wt.entity.dictionary.LockReason;
import wt.entity.dictionary.LockReasonExample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("dictionary/leaveReason")
public class LeaveReasonController extends BaseController {
    @Autowired
    LeaveReasonServices leaveReasonServices;

    @RequiresPermissions("dictionary:leaveReason:default")
    @RequestMapping("index")
    public String index() {
        return "dictionary/leaveReason/index";
    }

    @RequiresPermissions("dictionary:leaveReason:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            LeaveReason leaveReason;

            leaveReason = leaveReasonServices.selectByPrimaryKey(id);

            if (leaveReason == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, leaveReason);
            map.put(StaticConstant.EDIT_URL, "dictionary/leaveReason/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "dictionary/leaveReason/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "dictionary/leaveReason/editDlg";
    }

    @RequiresPermissions("dictionary:leaveReason:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult add(LeaveReason leaveReason) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(leaveReason.getName(), new NotBlankFluentValidator("名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        LeaveReasonExample leaveReasonExample=new LeaveReasonExample();
        leaveReasonExample.createCriteria().andNameEqualTo(leaveReason.getName());
        List<LeaveReason> leaveReasonList = leaveReasonServices.selectByExample(leaveReasonExample);
        if (leaveReasonList.isEmpty()){
            leaveReasonServices.insertSelective(leaveReason);
            return commonResult.setSuccess();
        }else {
            commonResult.setMessage("名称不能重复！");
            return commonResult.setError();
        }

    }


    @RequiresPermissions("dictionary:leaveReason:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(LeaveReason leaveReason) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(leaveReason.getName(), new NotBlankFluentValidator("名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        LeaveReasonExample leaveReasonExample=new LeaveReasonExample();
        leaveReasonExample.createCriteria().andNameEqualTo(leaveReason.getName()).andIdNotEqualTo(leaveReason.getId());
        List<LeaveReason> leaveReasonList = leaveReasonServices.selectByExample(leaveReasonExample);
        if (leaveReasonList.isEmpty()){
            leaveReasonServices.updateByPrimaryKeySelective(leaveReason);
            return commonResult.setSuccess();
        }else {
            commonResult.setMessage("名称不能重复！");
            return commonResult.setError();
        }


    }


    @RequiresPermissions("dictionary:leaveReason:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }

        leaveReasonServices.deleteByPrimaryKey(id);

        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:leaveReason:default")
    @ResponseBody
    @RequestMapping("select")
    public List<LeaveReason> select() throws Exception {

        LeaveReasonExample leaveReasonExample=new LeaveReasonExample();
        leaveReasonExample.setOrderByClause("id desc");
        return leaveReasonServices.selectByExample(leaveReasonExample);
    }





}
